home *** CD-ROM | disk | FTP | other *** search
- ///--------------------------------------------------------------------------------------
- // Simple.c
- ///--------------------------------------------------------------------------------------
-
- #include <SWIncludes.h> // Automatically include all SpriteWorld headers
- #include <SWGameUtils.h>
- #include <SWFPSReport.h>
-
- #include "SWApplication.h"
- #include "Simple.h"
-
-
- #define kFullScreenWindow false // Set to true if you want a window larger than 640x480
- #define kInterlacedMode false // You can use interlacing even with non-scrolling animations
- #define kSyncToVBL false // Syncs the animation to the VBL
- #define kWorldRectInset 0 // Makes the SpriteWorld smaller than the window.
- #define kMaxFPS 60 // Keep the animation from going too fast
-
- #define kSpriteCIconResID 128
- #define kNumSpriteFrames 1
-
- #define kNumberOfSprites 6
- #define kSpriteMoveTime 0
- #define kBaseMoveDelta 1
-
-
-
- ///--------------------------------------------------------------------------------------
- // main
- ///--------------------------------------------------------------------------------------
-
- void main(void)
- {
- WindowPtr myWindowP = NULL;
- Rect windRect;
- RgnHandle mBarUpdateRgn;
-
-
- Initialize(kNumberOfMoreMastersCalls);
-
- if (!SWHasSystem7())
- CantRunOnThisMachine();
-
-
- // Create and set up window
- myWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
-
- if (myWindowP != NULL)
- {
- if (kFullScreenWindow == true)
- {
- SizeWindow(myWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(myWindowP, 0, 0, false);
- }
- else
- {
- MoveWindow(myWindowP, 0, 0, false);
- windRect = myWindowP->portRect;
-
- // Make sure the window fits on the screen
- if (windRect.bottom > qd.screenBits.bounds.bottom ||
- windRect.right > qd.screenBits.bounds.right)
- {
- // Make it smaller so it fits on the screen
- SizeWindow(myWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(myWindowP, 0, 0, false);
- }
- else
- {
- // Center window in screen
- CenterRect(&windRect, &qd.screenBits.bounds);
- MoveWindow(myWindowP, windRect.left, windRect.top, false);
- }
- }
-
- ShowWindow(myWindowP);
- SetPort(myWindowP);
- mBarUpdateRgn = HideMenuBar(myWindowP); // Must be done *after* showing window!
- EraseRgn(mBarUpdateRgn);
-
- if (kInterlacedMode)
- PaintRect(&myWindowP->portRect); // Blacken window for Interlaced mode
- }
- else
- CantFindResource();
-
-
- PerformSimpleAnimation((CWindowPtr)myWindowP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // PerformSimpleAnimation
- ///--------------------------------------------------------------------------------------
-
- void PerformSimpleAnimation(CWindowPtr srcWindowP)
- {
- OSErr err;
- PixPatHandle pixPatH;
-
- SpriteWorldPtr spriteWorldP;
- SpriteLayerPtr spriteLayerP;
- SpritePtr simpleSpriteArray[kNumberOfSprites];
- SpritePtr simpleSpriteP;
-
- Rect moveBoundsRect;
- Rect worldRect;
- short spriteNum;
- unsigned long frames;
-
-
- SetCursor(*GetCursor(watchCursor));
-
-
- //
- // STEP #1: Initialize the sprite world package
- //
-
- err = SWEnterSpriteWorld();
- FatalError(err);
-
-
- //
- // STEP #2: Create the various pieces that we need
- //
-
-
- worldRect = srcWindowP->portRect;
- InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
-
- // Create the sprite world
- err = SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, &worldRect, NULL);
- FatalError(err);
-
-
- // Create the sprite layer
- err = SWCreateSpriteLayer(&spriteLayerP);
- FatalError(err);
-
-
- // Create the first sprite
- err = SWCreateSpriteFromCicnResource(spriteWorldP, &simpleSpriteP, NULL,
- kSpriteCIconResID, kNumSpriteFrames, kFatMask);
- FatalError(err);
-
- simpleSpriteArray[0] = simpleSpriteP;
-
- // clone the rest of the sprites off the first one
- for (spriteNum = 1; spriteNum < kNumberOfSprites; spriteNum++)
- {
- err = SWCloneSprite(simpleSpriteP, simpleSpriteArray + spriteNum, NULL);
- FatalError(err);
- }
-
-
- //
- // STEP #3: put the pieces together (must be done BEFORE the sprite world is locked!)
- //
-
- for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
- {
- // add the sprite to the layer
- SWAddSprite(spriteLayerP, simpleSpriteArray[spriteNum]);
- }
-
- // add the layer to the world
- SWAddSpriteLayer(spriteWorldP, spriteLayerP);
-
-
- //
- // STEP #4: set things up for the animation
- // (can be done before or after locking the SpriteWorld)
- //
-
- // calculate the movement boundary rectangle
- moveBoundsRect = spriteWorldP->backRect;
-
- for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
- {
- simpleSpriteP = simpleSpriteArray[spriteNum];
-
- // set up the sprite
- SWSetSpriteMoveBounds(simpleSpriteP, &moveBoundsRect);
- SWSetSpriteMoveTime(simpleSpriteP, kSpriteMoveTime);
- SWSetSpriteMoveProc(simpleSpriteP, BallSpriteMoveProc);
- SWSetSpriteMoveDelta(simpleSpriteP,
- kBaseMoveDelta+spriteNum, kBaseMoveDelta+spriteNum);
-
- // set the sprite’s initial location
- SWSetSpriteLocation(simpleSpriteP, spriteNum * 60, spriteNum * 60);
- }
-
-
- //
- // Use BlitPixie for the various drawProcs.
- //
-
- if (spriteWorldP->pixelDepth == 8)
- {
- SWCompileSprite(simpleSpriteArray[0]);
-
- if (kInterlacedMode)
- {
- SWSetSpriteWorldScreenDrawProc(spriteWorldP, BP8BitInterlacedRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BP8BitInterlacedRectDrawProc);
- }
- else
- {
- SWSetSpriteWorldScreenDrawProc(spriteWorldP, BlitPixie8BitRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BlitPixie8BitRectDrawProc);
- }
-
- for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
- {
- SWSetSpriteDrawProc(simpleSpriteArray[spriteNum], CompiledSprite8BitDrawProc);
- }
- }
- else // Not in 256 colors
- {
- #if !SW_PPC // no BlitPixieAllBit in PPC compile
- if (kInterlacedMode)
- {
- SWSetSpriteWorldScreenDrawProc(spriteWorldP, BPAllBitInterlacedRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BPAllBitInterlacedRectDrawProc);
- }
- else
- {
- SWSetSpriteWorldScreenDrawProc(spriteWorldP, BlitPixieAllBitRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(spriteWorldP, BlitPixieAllBitRectDrawProc);
- }
-
- for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
- {
- simpleSpriteP = simpleSpriteArray[spriteNum];
-
- if (kInterlacedMode)
- SWSetSpriteDrawProc(simpleSpriteP, BPAllBitInterlacedMaskDrawProc);
- else
- SWSetSpriteDrawProc(simpleSpriteP, BlitPixieAllBitMaskDrawProc);
- }
- #endif
- }
-
-
- //
- // STEP #5: Lock the sprite world. !!! VERY IMPORTANT !!!
- //
-
- SWLockSpriteWorld(spriteWorldP);
-
-
- //
- // SETP #6: Draw a nice background. Must be done *after* locking the SpriteWorld!
- //
-
- SWSetPortToBackground(spriteWorldP);
-
- if (spriteWorldP->pixelDepth == 1)
- pixPatH = GetPixPat(129); // B&W dithered background
- else
- pixPatH = GetPixPat(128); // Color
-
- if (pixPatH != NULL)
- {
- FillCRect(&moveBoundsRect, pixPatH);
- DisposePixPat(pixPatH);
- }
-
- SWSetPortToWindow(spriteWorldP);
-
-
- SWSetSpriteWorldMaxFPS(spriteWorldP, kMaxFPS);
- SWSyncSpriteWorldToVBL(spriteWorldP, kSyncToVBL);
-
- SetCursor(&qd.arrow);
- HideCursor();
-
- // Make sure CopyBits doesn't try to colorize things
- ForeColor(blackColor);
- BackColor(whiteColor);
-
-
- //
- // STEP #7: run the animation
- //
-
-
- SWUpdateSpriteWorld(spriteWorldP, true);
-
- frames = 0;
- StartTimer(); // For FPS report later
-
- while (!Button())
- {
- SWProcessSpriteWorld(spriteWorldP);
- SWAnimateSpriteWorld(spriteWorldP);
-
- if (spriteWorldP->frameHasOccured)
- frames++;
- }
-
- ShowMenuBar((WindowPtr)srcWindowP);
- ShowResults(frames); // Show FPS report
-
-
- //
- // STEP #8: Clean up
- //
-
- SWUnlockSpriteWorld(spriteWorldP);
- SWDisposeSpriteWorld(spriteWorldP);
- SWExitSpriteWorld();
-
- FlushEvents(everyEvent, 0);
- ShowCursor();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // BallSpriteMoveProc
- ///--------------------------------------------------------------------------------------
-
-
- void BallSpriteMoveProc(SpritePtr ballSpriteP)
- {
- SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
-
- SWBounceSprite(ballSpriteP);
- // SWWrapSprite(ballSpriteP);
- }
-
-